/* Code licensed under the BSD License: http://www.opensource.org/licenses/bsd-license.php */
/**
* @description <p>Lister singleton widget to list and edit inline PhoneBook entries.</p>
* @requires PhoneBook, ANVIL, YAHOO, ...
**/

if (typeof EDITOR == "undefined") {
    var EDITOR = {};
}

// -- API functions --- 

EDITOR.hired;

EDITOR.handleSelectHired = function (type,args,obj) {
    var dates = args[0];    
	var date = dates[0];
	var year = date[0], month = date[1], day = date[2];
    var hired = document.getElementById("hired");
    hired.value = month + "/" + day + "/" + year;
};
    
EDITOR.setValues = function (result) {
    var v = document.editorForm;     
        v.entry_key.value = result.entry_key;
        v.last_name.value = result.last_name;
        v.first_name.value = result.first_name;
        v.extension.value = result.extension;
        v.user_name.value = result.user_name;
        v.hired.value = result.hired;
        v.hours.value = result.hours;
        v.editor.value = result.editor;            
    EDITOR.hired = new YAHOO.widget.Calendar("hiredCalendar1","hiredCalendar");
    EDITOR.hired.cfg.setProperty("selected",v.hired.value);
    EDITOR.hired.render();    
    EDITOR.hired.selectEvent.subscribe(EDITOR.handleSelectHired, EDITOR.hired, true);
};
    
EDITOR.getValues = function () {
    var sDates = EDITOR.hired.getSelectedDates();
    var v = document.editorForm;                
    var result = { 
        "entry_key": v.entry_key.value, 
        "last_name": v.last_name.value, 
        "first_name": v.first_name.value, 
        "extension": v.extension.value, 
        "user_name": v.user_name.value, 
        "hired": v.hired.value, 
        "hours": v.hours.value, 
        "editor": v.editor.value};
   return result;
};
    
EDITOR.reset = function () {
var getToday = function () {
  var currentTime = new Date();
  var mm = currentTime.getMonth() + 1;
  var dd = currentTime.getDate();
  var yyyy = currentTime.getFullYear();
  if (mm.length==1) {mm = mm.concat('0',mm);}
  if (dd.length==1) {dd = mm.concat('0',dd);} 
  var today = mm + "/" + dd + "/" + yyyy;
  return today;
};

var default_values = { 
        "entry_key": "", 
        "last_name": "", 
        "first_name": "", 
        "extension": "", 
        "user_name": "", 
        "hired": getToday(), 
        "hours": 37.5, 
        "editor": '0'};

EDITOR.setValues(default_values);

};
    
        
// -- handlers and callbacks --

EDITOR.onCancel = function(p_oEvent) {  
    EDITOR.reset();
    LISTER.activate();
};

EDITOR.onAdd = function(p_oEvent) {  
    EDITOR.reset();
    EDITOR.activate();
};

EDITOR.onEdit_load = function (data) {    
    EDITOR.setValues(data.result);
    EDITOR.activate();
};


EDITOR.onDelete_load = function (data) {
    if ((data) && (data.result) && (data.result.user_name)) {
        ANVIL.setMessage("Deleted: " + data.result.user_name); 
        PhoneBook.rpc.entry_list(LISTER.onDeleteRefresh_load).call(ANVIL.channel);           
    }
    else {
        MENU.WorkflowHint();
    }
};

EDITOR.onSaveInsert_load = function (data) {    
    EDITOR.reset();
    var lister = LISTER.getDataTable();
    var rs = lister.getRecordSet();
    var record = rs.addRecord( data.result );
    lister.addRow( record );
    LISTER.scrollToEnd();
    LISTER.activate();
    ANVIL.setMessage("Added: " + data.result.user_name);
};

// See issue 18
EDITOR.onSaveUpdateRefresh_load = function (data) {
    LISTER.refresh(data);
    var message = "Saved entry and refreshed list";
    ANVIL.setMessage(message);  
    var lister = LISTER.getDataTable();      
    lister.showPage(lister.pageCurrent);
    LISTER.activate();
};

EDITOR.onSaveUpdate_load = function (data) {
    EDITOR.reset();
    ANVIL.setMessage("Edited: " + data.result.user_name);
    PhoneBook.rpc.entry_list(EDITOR.onSaveUpdateRefresh_load).call(ANVIL.channel);     
};

EDITOR.onSaveDelete_load = function (data) {
    EDITOR.reset();
    LISTER.refresh(data);
    LISTER.activate();
    ANVIL.setMessage("Deleted: " + data.result.user_name);
};

EDITOR.onSave = function (p_oEvent) {

     var values = EDITOR.getValues();

     if (values.entry_key) {
        PhoneBook.rpc.entry_save(values,EDITOR.onSaveUpdate_load).call(ANVIL.channel);
     }
     else {         
        PhoneBook.rpc.entry_save(values,EDITOR.onSaveInsert_load).call(ANVIL.channel);
     }
};

EDITOR.onEditorDelete = function (p_oEvent) {    
    var values = EDITOR.getValues();    
    LISTER.deleteConfirm(values);
    LISTER.activate();        
};

EDITOR.onCancel = function (p_oEvent) {
    EDITOR.reset();
    LISTER.activate();
};

// -- test methods --                

EDITOR.testData = { 
    "result" : {
        "entry_key": 'c5b6bbb1-66d6-49cb-9db6-743af6627829', 
        "last_name": 'Clinton', 
        "first_name": 'William', 
        "extension": '555\-743\-7828', 
        "user_name": 'bubba2', 
        "hired": '08/19/1992', 
        "hours": 37.5, 
        "editor": '0'}};

EDITOR.test = function () {
    EDITOR.onSaveInsert_load(testData);       
    alert("ready");        
    LISTER.onRefresh_load(testData);
};

// -- event bindings -- 

MENU.addEvent.subscribe(EDITOR.onAdd, MENU); 
MENU.saveEvent.subscribe(EDITOR.onSave, MENU); 
MENU.editorDeleteEvent.subscribe(EDITOR.onEditorDelete, MENU); 
MENU.cancelEvent.subscribe(EDITOR.onCancel, MENU); 
